package com.lzhl.buildjar.ui.support;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

/**
 *
 * @author 李柱良(@柱子的日月)
 * @date 2014/6/19
 */
public class BaseDialog extends JDialog {

    public BaseDialog(Frame owner, boolean modal) {
        super(owner, modal);
        init();
    }

    public BaseDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        init();
    }

    public BaseDialog(Dialog owner, boolean modal) {
        super(owner, modal);
        init();
    }

    private void init() {
        InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "dispose");

        ActionMap actionMap = getRootPane().getActionMap();
        actionMap.put("dispose", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                if(canExit()) {
                    exit();
                }
            }
        });
    }

    protected boolean canExit() {
        return true;
    }

    public void exit() {
        setVisible(false);
        dispose();
    }
}
